﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlogEngine.Core;
using System.Web.UI;
using _4mvcBlog.Core;
using _4mvcBlog.Core.ViewEngine;
using System.Web.UI.WebControls;

namespace _4mvcBlog.Controllers
{
    public partial class AdminController
    {
        #region Blog设置

        [AcceptVerbs("GET")]
        public ActionResult Controls()
        {
            return View(BlogSettings.Instance);
        }

        [AcceptVerbs("GET")]
        public ActionResult Settings()
        {
            ViewData["Themes"] = BindThemes();
            ViewData["Cultures"] = BindCultures();

            return View(BlogSettings.Instance);
        }

        [ActionName("Settings"), AcceptVerbs("POST")]
        [TestUserAuthorize]
        public ActionResult SaveSettings()
        {
            //当然你可以这样取值
            //string name = Request.Form["Name"];

            //但我们有更简便的：
            try
            {
                UpdateModel(BlogSettings.Instance);
            }
            catch
            {
                ViewData["Themes"] = BindThemes();
                ViewData["Cultures"] = BindCultures();
                return View(BlogSettings.Instance);
            }

            //在这里你可能还需要对自定义的业务逻辑进行验证

            BlogSettings.Instance.Save();

            (ViewEngines.Engines[1] as ThemeViewEngine).Theme = BlogSettings.Instance.Theme;

            return ShowMsg(new List<string>() { "修改设置成功" });
        }
        /// <summary>
        /// 生成主题目录下的主题列表
        /// </summary>
        /// <returns></returns>
        private SelectList BindThemes()
        {
            List<ListItem> themes = new List<ListItem>();
            string themePath = Server.MapPath(Utils.RelativeWebRoot + "Views/themes/");
            foreach (string dic in System.IO.Directory.GetDirectories(themePath))//主题下拉列表
            {
                int index = dic.LastIndexOf(System.IO.Path.DirectorySeparatorChar) + 1;
                string theme = dic.Substring(index);
                if (theme.Equals("shared", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }
                themes.Add(new ListItem { Text = theme, Value = theme });
            }
            return new SelectList(themes, "Value", "Text", BlogSettings.Instance.Theme);
        }
        /// <summary>
        /// 生成本地化语言列表
        /// </summary>
        /// <returns></returns>
        private SelectList BindCultures()
        {
            List<ListItem> cultures = new List<ListItem>();
            //cultures.Add("Auto", "");
            cultures.Add(new ListItem { Text = "english", Value = "en" });
            if (System.IO.File.Exists(System.IO.Path.Combine(HttpRuntime.AppDomainAppPath, "PrecompiledApp.config")))
            {

                string precompiledDir = HttpRuntime.BinDirectory;
                string[] translations = System.IO.Directory.GetFiles(precompiledDir, "App_GlobalResources.resources.dll", System.IO.SearchOption.AllDirectories);
                foreach (string translation in translations)
                {
                    string resourceDir = System.IO.Path.GetDirectoryName(translation).Remove(0, precompiledDir.Length);
                    if (!String.IsNullOrEmpty(resourceDir))
                    {

                        System.Globalization.CultureInfo info = System.Globalization.CultureInfo.GetCultureInfoByIetfLanguageTag(resourceDir);
                        cultures.Add(new ListItem { Text = info.NativeName, Value = resourceDir });
                    }
                }
            }
            else
            {

                string culturePath = Server.MapPath(Utils.RelativeWebRoot + "App_GlobalResources/");
                foreach (string file in System.IO.Directory.GetFiles(culturePath, "labels.*.resx"))
                {

                    int index = file.LastIndexOf(System.IO.Path.DirectorySeparatorChar) + 1;
                    string filename = file.Substring(index);
                    filename = filename.Replace("labels.", string.Empty).Replace(".resx", string.Empty);
                    System.Globalization.CultureInfo info = System.Globalization.CultureInfo.GetCultureInfoByIetfLanguageTag(filename);
                    cultures.Add(new ListItem { Text = info.NativeName, Value = filename });

                }
            }
            return new SelectList(cultures, "Value", "Text", BlogSettings.Instance.Culture);
        }
        #endregion
    }
}
